<template>
  <el-dialog
    :close-on-click-modal="false"
    title="开标纪要"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="110px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="招标编号" prop="bidNumber">
            <!-- <el-input v-model="form.bidNumber" :disabled="disableForm" /> -->
            <el-input v-model="form.bidNumber" readonly :disabled="disableForm || optType !== 'add'" @click.native="optType === 'add' ? showListModal = true : null">
              <i slot="suffix" class="el-input__icon el-icon-circle-plus-outline" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标名称" prop="bidName">
            <el-input v-model="form.bidName" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标机构" prop="bidOrganization">
            <el-input v-model="form.bidOrganization" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="招标类型" prop="bidType">
            <el-select v-model="form.bidType" :disabled="disableForm" style="width: 100%;" placeholder="请选择招标类型">
              <el-option v-for="item in bidOpenInfoBidType" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开标时间" prop="basStartTime">
            <el-date-picker v-model="form.basStartTime" style="width:100%" type="date" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开标地点" prop="basAddress">
            <el-input v-model="form.basAddress" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系人" prop="basContactPerson">
            <el-input v-model="form.basContactPerson" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系电话" prop="basContactPhone">
            <el-input v-model="form.basContactPhone" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注" prop="basRemark">
            <el-input v-model="form.basRemark" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="中标价格" prop="basBidAmount">
            <el-input v-model="form.basBidAmount" disabled />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="8">
          <el-form-item label="供应商信息Id" prop="supplierInfoId">
            <el-input v-model="form.supplierInfoId" :disabled="disableForm" />
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="供应商名称" prop="supplierInfoName">
            <el-input v-model="form.supplierInfoName" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <div style="padding-bottom: 10px;">
        <el-button type="primary" @click="showTargetSetListModal = true">引入供应商</el-button>
        <el-button type="primary" @click="deleteDetail">删除供应商</el-button>
      </div>
      <com-table
        v-model="selectRow"
        highlight-current-row
        :data="targetList"
        :columns="columns"
        :no-page="true"
      >
        <template v-slot:bidOpenInfoId="{row}">
          <el-input v-model="row.bidOpenInfoId" />
        </template>
        <template v-slot:contactPerson="{row}">
          <el-input v-model="row.contactPerson" />
        </template>
        <template v-slot:supplierInfoFraction="{row}">
          <el-input v-model="row.supplierInfoFraction" />
        </template>
        <template v-slot:contactPhone="{row}">
          <el-input v-model="row.contactPhone" />
        </template>
        <template v-slot:quotationPrice="{row}">
          <el-input v-model="row.quotationPrice" />
        </template>
        <template v-slot:rank="{row}">
          <el-input v-model="row.rank" :disabled="true" />
        </template>
        <template v-slot:remark="{row}">
          <el-input v-model="row.remark" />
        </template>
        <template v-slot:win="{row}">
          <el-select v-model="row.win" placeholder="是否中标" @change="select(row.supplierInfoId)">
            <el-option label="是" :value="true" />
            <el-option label="否" :value="false" />
          </el-select>
        </template>
      </com-table>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
    <supplierList title="供应商列表" :show-modal.sync="showTargetSetListModal" :columns="columns" :api-obj="{api:getSupplierFinishInfoList}" @getSelectedRows="getSelectedRows" />
    <listSelect :show-modal.sync="showListModal" title="招标文本选择" :columns="listColumns" :api="getBidTextFinishInfoList" @getSelectedData="getSelectedBidText" />

  </el-dialog>
</template>

<script>
import { addBidOpenInfo, updateBidOpenInfo, getBidOpenInfoDetail, getBidTextFinishInfoList } from '@/api/bigManage'
import { getAreaList } from '@/api/common'
import { getDictValue } from '@/utils/comm'
import { bidOpenInfoBidType, workflowStatus } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
import supplierList from './supplierList'
import { getSupplierFinishInfoList } from '@/api/bigManage'
import { turnMap, bidOpenInfo } from './config.js'
import listSelect from '@/components/ListSelect'
export default {
  name: 'AddBidOpenInfo',
  components: {
    fileUploader,
    supplierList,
    listSelect
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      getSupplierFinishInfoList,
      getBidTextFinishInfoList,
      showListModal: false,
      showFileModal: false,
      selectRow: null,
      targetList: [],
      bidOpenInfoBidType,
      workflowStatus,
      getDictValue,
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        bidInfoId: '',
        bidOrganization: '',
        bidNumber: '',
        bidName: '',
        bidType: '',
        basStartTime: '',
        basAddress: '',
        basContactPerson: '',
        basContactPhone: '',
        basRemark: '',
        basBidAmount: '',
        supplierInfoId: '',
        supplierInfoName: ''
      },
      listColumns: [
        {
          label: '招标机构',
          prop: 'bidOrganization'
        },
        {
          label: '招标编号',
          prop: 'bidNumber',
          width: '160'
        },
        {
          label: '招标名称',
          prop: 'bidName',
          width: '150'
        },
        {
          label: '招标类型',
          prop: 'bidType'
        },
        {
          label: '招标完成时间',
          prop: 'basBidFinishTime',
          width: '100'
        },
        {
          label: '招标负责人',
          prop: 'basBidPerson',
          width: '100'
        },
        {
          label: '编制人',
          prop: 'founderName'
        },
        {
          label: '描述',
          prop: 'basDescription'
        },
        {
          label: '资质等级',
          prop: 'basQualificationLevel'
        },
        {
          label: '注册资金(万元)',
          prop: 'basRegisteredCapital',
          width: '120'
        },
        {
          label: '投标保证金(万元)',
          prop: 'basBidMargin',
          width: '120'
        },
        {
          label: '编制时间',
          prop: 'createTime',
          width: '90'
        }
      ],
      columns: [
        {
          label: '供应商名称',
          prop: 'supplierInfoName'
        },
        {
          label: '投标报价',
          prop: 'quotationPrice',
          slot: 'quotationPrice'
        },
        {
          label: '评标得分',
          prop: 'supplierInfoFraction',
          slot: 'supplierInfoFraction'
        },
        {
          label: '是否中标',
          prop: 'win',
          slot: 'win',
          width: 200
        },
        {
          label: '排名',
          prop: 'rank',
          slot: 'rank'
        },
        {
          label: '联系人',
          prop: 'contactPerson'
        },
        {
          label: '联系电话',
          prop: 'contactPhone'
        },
        {
          label: '备注',
          prop: 'remark',
          slot: 'remark'
        }
        // {
        //   fixed: 'right',
        //   prop: 'options',
        //   slot: 'options'
        // }
      ],
      showTargetSetListModal: false,
      fileList: [
      ],
      rules: {
        supplierInfoName: [
          { required: true, message: '供应商名称', trigger: 'change' }
        ],
        basBidAmount: [
          { required: true, message: '中标价格', trigger: 'change' }
        ],
        basContactPerson: [
          { required: true, message: '请输入联系人', trigger: 'change' }
        ],
        basContactPhone: [
          { required: true, message: '请输入联系电话', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            const source = this.data[key]
            if (typeof source === 'boolean') {
              this.form[key] = source ? 1 : 0
            } else {
              this.form[key] = source
            }
          })
        })
      } else if (!val) {
        this.fileList = []
        this.targetList = []
        this.$refs.form.resetFields()
      }
    },
    selectRow: {
      handler(val) {
        if (val) {
          this.form.supplierInfoId = val.supplierInfoId
          // this.form.supplierInfoName = val.supplierInfoName
          // this.form.basBidAmount = val.quotationPrice
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    select(id) {
      this.targetList.forEach(item => {
        if (item.supplierInfoId === id) {
          item.win = true

          this.form.supplierInfoName = item.supplierInfoName
          this.form.basBidAmount = item.quotationPrice
        } else {
          item.win = false
        }
      })
      this.$forceUpdate()
    },
    getSelectedBidText(data) {
      if (data) {
        // this.supplierInfo = data
        const { bidName, bidNumber, bidOrganization, bidType, bidInfoId } = data
        this.form.bidInfoId = bidInfoId
        this.form.bidName = bidName
        this.form.bidNumber = bidNumber
        this.form.bidOrganization = bidOrganization
        this.form.bidType = this.getDictValue(this.bidOpenInfoBidType, bidType)
      }
    },
    getSelectedRows(data) {
      const noIncludesData = data.filter(item => !this.targetList.some(v => v.supplierInfoId === item.id))
      this.targetList = this.targetList.concat(noIncludesData.map(item => {
        const { name, id, contactName, contactPhone } = item
        return {
          supplierInfoId: id,
          supplierInfoName: name,
          quotationPrice: '',
          win: '',
          rank: '',
          contactPerson: contactName,
          contactPhone: contactPhone,
          remark: ''
        }
      }))
    },
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getBidOpenInfoDetail({ bidOpenInfoId: id }).then(res => {
        const { lstAttachment, lstBidOpenDetail } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        if (lstBidOpenDetail && lstBidOpenDetail.length > 0) {
          this.targetList = lstBidOpenDetail
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    deleteDetail() {
      if (this.selectRow) {
        const index = this.targetList.indexOf(this.selectRow)
        this.targetList.splice(index, 1)
      } else {
        this.$message({
          type: 'success',
          message: '请选中删除行!'
        })
      }
    },
    // 数据处理
    dealData(obj) {
      const tmpObj = this.optType === 'edit' ? {
        bidOpenInfoId: this.data.id,
        id: obj.id
      } : {}
      for (const key in bidOpenInfo) {
        const val = turnMap[key]
        tmpObj[key] = obj[key] ? obj[key] : obj[val]
      }
      return tmpObj
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const tmpArr = []
          this.targetList.map(item => {
            const tmpObj = this.dealData(item)
            tmpArr.push(tmpObj)
          })
          const params = {
            bidOpenInfo: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList,
            lstBidOpenDetail: tmpArr
          }
          const fn = this.optType === 'add' ? addBidOpenInfo : updateBidOpenInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
